package edu.tyut.game.logic

import edu.tyut.game.logic.Repository.fire
import edu.tyut.game.logic.dao.GameDB
import edu.tyut.game.logic.dao.GameDB.existUser
import edu.tyut.game.logic.dao.GameDB.registerDB
import edu.tyut.game.logic.model.User
import edu.tyut.game.logic.network.GameNetWork
import edu.tyut.game.utils.LogUtils

object PoemRepository {

    private const val tag = "PoemRepository"

    /**
     * 获取朝代信息
     */
    fun getDynasties() = fire {
        val res = GameNetWork.getDynasties()
        if (res.status == "ok") {
            Result.success(res.data)
        } else {
            Result.failure(Exception("获取朝代失败: ${res.msg}"))
        }
    }

    /**
     * 获取诗词排序
     */
    fun getPoemOrderByClickCount() = fire {
        val res = GameNetWork.getPoemOrderByClickCount()
        if (res.status == "ok") {
            Result.success(res.data)
        } else {
            Result.failure(Exception("获取诗词排序失败: ${res.msg}"))
        }
    }

    fun getPoemListByDynasty(dynasty: String) = fire {
        val res = GameNetWork.getPoemList(dynasty)
        if (res.status == "ok"){
            Result.success(res.data)
        }else{
            LogUtils.d(tag, res.msg)
            Result.failure(java.lang.Exception("获取是此列表失败： ${res.msg}"))
        }
    }

    fun getPoemListBySearch(content: String) = fire {
        val res = GameNetWork.getSearchPoemList(content)
        if (res.status == "ok"){
            Result.success(res.data)
        }else{
            LogUtils.d(tag, res.msg)
            Result.failure(Exception("获取搜索诗词失败： ${res.msg}"))
        }
    }

    fun poemRecommend(ids: String) = fire {
        val res = GameNetWork.poemRecommend(ids)
        if (res.status == "ok"){
            Result.success(res.data)
        }else{
            LogUtils.d(tag, res.msg)
            Result.failure(Exception("埋点失败： ${res.msg}"))
        }
    }
}